Completed
Pull Request — future (#174)
by Xaver
30s
created

node.js ➔ ... ➔ showGateway   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
dl 0
loc 24
rs 8.9713
nop 1
1
define(['sorttable', 'snabbdom', 'd3-interpolate', 'moment', 'helper'],
2
  function (SortTable, V, d3Interpolate, moment, helper) {
3
    'use strict';
4
    V = V.default;
5
6
    function showGeoURI(d) {
7
      if (!helper.hasLocation(d)) {
8
        return undefined;
9
      }
10
11
      return V.h('td',
12
        V.h('a',
13
          { props: { href: 'geo:' + d.location.latitude + ',' + d.location.longitude } },
14
          Number(d.location.latitude.toFixed(6)) + ', ' + Number(d.location.longitude.toFixed(6))
15
        )
16
      );
17
    }
18
19
    function showStatus(d) {
20
      return V.h('td',
21
        { props: { className: d.is_unseen ? 'unseen' : (d.is_online ? 'online' : 'offline') } },
22
        _.t((d.is_online ? 'node.lastOnline' : 'node.lastOffline'), {
23
          time: d.lastseen.fromNow(),
24
          date: d.lastseen.format('DD.MM.YYYY, H:mm:ss')
25
        }));
26
    }
27
28
    function showFirmware(d) {
29
      return [
30
        helper.dictGet(d, ['firmware', 'release']),
31
        helper.dictGet(d, ['firmware', 'base'])
32
      ].filter(function (n) {
33
        return n !== null;
34
      }).join(' / ') || undefined;
35
    }
36
37
    function showSite(d, config) {
38
      var site = helper.dictGet(d, ['site_code']);
39
      var rt = site;
40
      if (config.siteNames) {
41
        config.siteNames.forEach(function (t) {
42
          if (site === t.site) {
43
            rt = t.name;
44
          }
45
        });
46
      }
47
      return rt || undefined;
48
    }
49
50
    function showUptime(d) {
51
      if (!('uptime' in d)) {
52
        return undefined;
53
      }
54
55
      return moment.utc(d.uptime).local().fromNow(true);
56
    }
57
58
    function showFirstseen(d) {
59
      if (!('firstseen' in d)) {
60
        return undefined;
61
      }
62
63
      return d.firstseen.fromNow(true);
64
    }
65
66
    function showClients(d) {
67
      if (!d.is_online) {
68
        return undefined;
69
      }
70
71
      var clients = [
72
        V.h('span', [
73
          d.clients > 0 ? d.clients : _.t('none'),
74
          V.h('br'),
75
          V.h('i', { props: { className: 'ion-people', title: _.t('node.clients') } })
76
        ]),
77
        V.h('span',
78
          { props: { className: 'legend-24ghz' } },
79
          [
80
            d.clients_wifi24,
81
            V.h('br'),
82
            V.h('span', { props: { className: 'symbol', title: '2,4 Ghz' } })
83
          ]),
84
        V.h('span',
85
          { props: { className: 'legend-5ghz' } },
86
          [
87
            d.clients_wifi5,
88
            V.h('br'),
89
            V.h('span', { props: { className: 'symbol', title: '5 Ghz' } })
90
          ]),
91
        V.h('span',
92
          { props: { className: 'legend-others' } },
93
          [
94
            d.clients_other,
95
            V.h('br'),
96
            V.h('span', { props: { className: 'symbol', title: _.t('others') } })
97
          ])
98
      ];
99
100
      return V.h('td', { props: { className: 'clients' } }, clients);
101
    }
102
103
    function showIPs(d) {
104
      var ips = helper.dictGet(d, ['network', 'addresses']);
105
      if (ips === null) {
106
        return undefined;
107
      }
108
109
      ips.sort();
110
111
      var string = [];
112
      ips.forEach(function (ip, i) {
113
        var link = !ip.startsWith('fe80:');
114
115
        if (i > 0) {
116
          string.push(V.h('br'));
117
        }
118
119
        if (link) {
120
          string.push(V.h('a', { props: { href: 'http://[' + ip + ']/', target: '_blank' } }, ip));
121
        } else {
122
          string.push(ip);
123
        }
124
      });
125
      return V.h('td', string);
126
    }
127
128
    function showBar(v, width, warning) {
129
      return V.h('span',
130
        { props: { className: 'bar' + (warning ? ' warning' : '') } },
131
        [
132
          V.h('span',
133
            {
134
              style: { width: (width * 100) + '%' }
135
            }),
136
          V.h('label', v)
137
        ]
138
      );
139
    }
140
141
    function showLoad(d) {
142
      if (!('loadavg' in d)) {
143
        return undefined;
144
      }
145
146
      var value = d.loadavg.toFixed(2);
147
      var width = d.loadavg % 1;
148
      var warning = false;
149
      if (d.loadavg >= d.nproc) {
150
        warning = true;
151
      }
152
      return showBar(value, width, warning);
153
    }
154
155
    function showRAM(d) {
156
      if (!('memory_usage' in d)) {
157
        return undefined;
158
      }
159
160
      var value = Math.round(d.memory_usage * 100) + ' %';
161
      var width = d.memory_usage;
162
      var warning = false;
163
      if (d.memory_usage >= 0.8) {
164
        warning = true;
165
      }
166
      return showBar(value, width, warning);
167
    }
168
169
    function showAutoupdate(d) {
170
      var au = helper.dictGet(d, ['autoupdater']);
171
      if (!au) {
172
        return undefined;
173
      }
174
175
      return au.enabled ? _.t('node.activated', { branch: au.branch }) : _.t('node.deactivated');
176
    }
177
178
    function showStatImg(o, d) {
179
      var subst = {};
180
      subst['{NODE_ID}'] = d.node_id;
181
      subst['{NODE_NAME}'] = d.hostname.replace(/[^a-z0-9\-]/ig, '_');
182
      subst['{TIME}'] = d.lastseen.format('DDMMYYYYHmmss');
183
      subst['{LOCALE}'] = _.locale();
184
      return helper.showStat(V, o, subst);
185
    }
186
187
    return function (config, el, router, d, linkScale, nodeDict) {
188
      function nodeLink(node) {
189
        return V.h('a', {
190
          props: {
191
            className: node.is_online ? 'online' : 'offline',
192
            href: router.generateLink({ node: node.node_id })
193
          }, on: {
194
            click: function (e) {
195
              router.fullUrl({ node: node.node_id }, e);
196
            }
197
          }
198
        }, node.hostname);
199
      }
200
201
      function nodeidLink(nodeid) {
202
        if (nodeDict[nodeid]) {
203
          return nodeLink(nodeDict[nodeid]);
204
        }
205
        return nodeid;
206
      }
207
208
      function showGateway(node) {
209
        var gatewayCols = [
210
          V.h('span', [
211
            nodeidLink(node.gateway_nexthop),
212
            V.h('br'),
213
            _.t('node.nexthop')
214
          ]),
215
          V.h('span', [
216
            V.h('i', { props: { className: 'ion-arrow-right-c' } })
217
          ]),
218
          V.h('span', [
219
            nodeidLink(node.gateway),
220
            V.h('br'),
221
            'IPv4'
222
          ]),
223
          V.h('span', [
224
            nodeidLink(node.gateway6),
225
            V.h('br'),
226
            'IPv6'
227
          ])
228
        ];
229
230
        return V.h('td', { props: { className: 'gateway' } }, gatewayCols);
231
      }
232
233
      function renderNeighbourRow(n) {
234
        var icons = [];
235
        if (helper.hasLocation(n.node)) {
236
          icons.push(V.h('span', { props: { className: 'ion-location' } }));
237
        }
238
239
        var td1 = V.h('td', icons);
240
        var td2 = V.h('td', nodeLink(n.node));
241
        var td3 = V.h('td', (n.node.clients ? n.node.clients.toString() : '0'));
242
        var td4 = V.h('td', { style: { color: linkScale((n.link.source_tq + n.link.target_tq) / 2) } }, helper.showTq(n.link.source_tq) + ' - ' + helper.showTq(n.link.target_tq));
243
        var td5 = V.h('td', helper.showDistance(n.link));
244
245
        return V.h('tr', [td1, td2, td3, td4, td5]);
246
      }
247
248
      var self = this;
249
      var header = document.createElement('h2');
250
      var table = document.createElement('table');
251
      var images = document.createElement('div');
252
      var neighbours = document.createElement('h3');
253
      var headings = [{
254
        name: ''
255
      }, {
256
        name: 'node.nodes',
257
        sort: function (a, b) {
258
          return a.node.hostname.localeCompare(b.node.hostname);
259
        },
260
        reverse: false
261
      }, {
262
        name: 'node.clients',
263
        class: 'ion-people',
264
        sort: function (a, b) {
265
          return ('clients' in a.node ? a.node.clients : -1) -
266
            ('clients' in b.node ? b.node.clients : -1);
267
        },
268
        reverse: true
269
      }, {
270
        name: 'node.tq',
271
        class: 'ion-connection-bars',
272
        sort: function (a, b) {
273
          return a.link.source_tq - b.link.source_tq;
274
        },
275
        reverse: true
276
      }, {
277
        name: 'node.distance',
278
        class: 'ion-arrow-resize',
279
        sort: function (a, b) {
280
          return (a.link.distance === undefined ? -1 : a.link.distance) -
281
            (b.link.distance === undefined ? -1 : b.link.distance);
282
        },
283
        reverse: true
284
      }];
285
      var tableNeighbour = new SortTable(headings, 1, renderNeighbourRow);
286
287
      el.appendChild(header);
288
      el.appendChild(table);
289
      el.appendChild(neighbours);
290
      el.appendChild(tableNeighbour.el);
291
      el.appendChild(images);
292
293
      self.render = function render() {
294
        V.patch(header, V.h('h2', d.hostname));
295
296
        var children = [];
297
298
        children.push(helper.attributeEntry(V, 'node.status', showStatus(d)));
299
        children.push(helper.attributeEntry(V, 'node.gateway', d.is_gateway ? 'ja' : null));
300
        children.push(helper.attributeEntry(V, 'node.coordinates', showGeoURI(d)));
301
302
        if (config.nodeInfobox && config.nodeInfobox.contact) {
303
          children.push(helper.attributeEntry(V, 'node.contact', helper.dictGet(d, ['owner', 'contact'])));
304
        }
305
306
        children.push(helper.attributeEntry(V, 'node.hardware', helper.dictGet(d, ['model'])));
307
        children.push(helper.attributeEntry(V, 'node.primaryMac', helper.dictGet(d, ['network', 'mac'])));
308
        children.push(helper.attributeEntry(V, 'node.firmware', showFirmware(d)));
309
        children.push(helper.attributeEntry(V, 'node.site', showSite(d, config)));
310
        children.push(helper.attributeEntry(V, 'node.uptime', showUptime(d)));
311
        children.push(helper.attributeEntry(V, 'node.firstSeen', showFirstseen(d)));
312
        if (config.nodeInfobox && config.nodeInfobox.hardwareUsage) {
313
          children.push(helper.attributeEntry(V, 'node.systemLoad', showLoad(d)));
314
          children.push(helper.attributeEntry(V, 'node.ram', showRAM(d)));
315
        }
316
        children.push(helper.attributeEntry(V, 'node.ipAddresses', showIPs(d)));
317
        children.push(helper.attributeEntry(V, 'node.update', showAutoupdate(d)));
318
        children.push(helper.attributeEntry(V, 'node.clients', showClients(d)));
319
        children.push(helper.attributeEntry(V, 'node.gateway', showGateway(d)));
320
321
        var elNew = V.h('table', children);
322
        table = V.patch(table, elNew);
323
        table.elm.classList.add('attributes');
324
325
        V.patch(neighbours, V.h('h3', _.t('node.link', d.neighbours.length) + ' (' + d.neighbours.length + ')'));
326
        if (d.neighbours.length > 0) {
327
          tableNeighbour.setData(d.neighbours);
328
          tableNeighbour.el.elm.classList.add('node-links');
329
        }
330
331
        if (config.nodeInfos) {
332
          var img = [];
333
          config.nodeInfos.forEach(function (nodeInfo) {
334
            img.push(V.h('h4', nodeInfo.name));
335
            img.push(showStatImg(nodeInfo, d));
336
          });
337
          images = V.patch(images, V.h('div', img));
338
        }
339
      };
340
341
      self.setData = function setData(data) {
342
        d = data.nodes.all.find(function (a) {
343
          return a.node_id === d.node_id;
344
        });
345
        self.render();
346
      };
347
      return self;
348
    };
349
  });
350